<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            top: 50px;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        
        span {
            position: absolute;
            left: 0;
            top: 200px;
            width: 100px;
            height: 100px;
            background-color: rgb(171, 44, 135);
        }
        
        button {
            width: 50px;
            height: 20px;
        }
    </style>
</head>

<body>
    <button id="b500">500</button>
    <button id="b900">900</button>
    <div></div>
    <span>夏</span>
    <script>
        function animate(obj, target, callback) {
            clearInterval(obj.time);
            obj.time = setInterval(function() {
                var step = (target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (obj.offsetLeft == target) {
                    clearInterval(obj.time);
                    if (callback) {
                        callback();
                    }
                }
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 30);
        }
        var div = document.querySelector('div');
        var span = document.querySelector('span');
        var b500 = document.querySelector('#b500');
        var b900 = document.querySelector('#b900');
        animate(div, 900);
        b500.addEventListener('click', function() {
            animate(span, 500);
        });
        b900.addEventListener('click', function() {
            animate(span, 900, function() {
                span.style.backgroundColor = 'red';
            });
        });
    </script>
</body>

</html>